package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.admin.dtos.SensitiveDto;
import com.heima.model.admin.dtos.SensitivePageDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;

@Service
@Slf4j
@Transactional
public class WmsensitiveService extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitiveService {


    /**
     * 保存
     *
     * @param wmSensitive
     * @return
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    @Override
    public ResponseResult create(WmSensitive wmSensitive) throws InvocationTargetException, IllegalAccessException {
//       if (wmSensitive==null){
//           return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
//       }
//       ResponseResult responseResult=new ResponseResult();
//       //创建条件构造器
//        LambdaQueryWrapper<WmSensitive> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.eq(WmSensitive::getSensitives,wmSensitive.getSensitives());
//
//        WmSensitive sensitive = getOne(lambdaQueryWrapper);
//
//        if (sensitive!=null){
//            responseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"已存在");
//        }
//
//        wmSensitive.setCreatedTime(new Date());
//
//        save(wmSensitive);
//        return responseResult.ok(AppHttpCodeEnum.SUCCESS);
        if (wmSensitive == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        LambdaQueryWrapper<WmSensitive> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(WmSensitive::getSensitives, wmSensitive.getSensitives());
        WmSensitive wmSensitive1 = getOne(lambdaQueryWrapper);
        if (wmSensitive1 != null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        wmSensitive.setSensitives(wmSensitive.getSensitives());
        wmSensitive.setCreatedTime(new Date());
        save(wmSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult del(Integer id) {

        boolean b = removeById(id);
        ResponseResult responseResult = new ResponseResult();
        return responseResult.ok(b);

    }

    @Override
    public ResponseResult findlist(SensitivePageDto sensitivePageDto) {
        sensitivePageDto.checkParam();
        IPage page = new Page(sensitivePageDto.getPage(), sensitivePageDto.getSize());
        LambdaQueryWrapper<WmSensitive> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByDesc(WmSensitive::getCreatedTime);
        if (StringUtils.isNotBlank(sensitivePageDto.getName())) {
            lambdaQueryWrapper.like(WmSensitive::getSensitives, sensitivePageDto.getName());
        }
        page(page, lambdaQueryWrapper);
        ResponseResult responseResult = new PageResponseResult(sensitivePageDto.getPage(), sensitivePageDto.getSize(), (int) page.getTotal());
        responseResult.setData(page.getRecords());
        return responseResult;

    }

    @Override
    public ResponseResult update(SensitiveDto sensitiveDto) throws InvocationTargetException, IllegalAccessException {
//        LambdaQueryWrapper<WmSensitive> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.eq(WmSensitive::getId,sensitiveDto.getId());
//        WmSensitive wmSensitive = getOne(lambdaQueryWrapper);
//        if (wmSensitive==null){
//            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
//        }
//        wmSensitive.setSensitives(sensitiveDto.getSensitives());
//        wmSensitive.setCreatedTime(new Date());
//        updateById(wmSensitive);
//        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
//    }
        LambdaQueryWrapper<WmSensitive> lamb = new LambdaQueryWrapper<>();
        lamb.eq(WmSensitive::getId, sensitiveDto.getId());
        WmSensitive wmSensitive = getOne(lamb);
        wmSensitive.setSensitives(sensitiveDto.getSensitives());
        wmSensitive.setCreatedTime(new Date());
        updateById(wmSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
